# pip install asyncio
# pip install websockets
# pip install websockets_routes

import asyncio
import websockets
import websockets_routes
import json

# 初始化一个router对象
router = websockets_routes.Router()

@router.route("/deep")  
async def echo(websocket, path):
    async for message in websocket:
        print("server已收到消息:{}".format(message))         
 
        ren = {"data":{"base_vol": 1, "close": 100, "coinCode": "221914_CNY", "count": 1, "high": 100, "id": 1668441600, "low": 100,"open":100,"quote_vol": 0,"seq": 1668441600,"type":"node"},"type": "node"}
        await websocket.send(str(json.dumps(ren)))
 

@router.route("/deep/{status}") #添加router的route装饰器，它会路由uri。
async def light_status(websocket, path):
    async for message in websocket:
        print("server已收到消息:{}".format(message))
        print(path.params['status'])        
 
        if (path.params['status'] == 'deep'):
            # /deep/deep 时的回复
            
            ren = {"area": [{"areaname": "CNY","areanameview": "CNY","data":[{"RiseAndFall":0,"billName": "102101A","billType":"0","coinCode": "102101A_CNY","coinSymbol": "","currentExchangPrice":"100.00","currentStatus":-2
                                                                          ,"keepDecimalCoinCode":2,"lastExchangPrice":"200.00","maxPrice": "100","minPrice":"100","name":"102101A_CNY","openPrice":"100.00"
                                                                          ,"points": "100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100"
                                                                          ,"rmbPrice": "1","transactionSum": "9.00","usdt_maxPrice":0,"usdt_minPrice":0,"usdt_yesterdayPrice":0,"usdtcount":0,"usdttormb" : 6.8
                                                                          ,"yesterdayPrice" :"100.00"}]}]
                                                                          ,"buySellData":[{"idCur":1672987115,"idPrev":0,"msgType":"marketDetail0","symbolId":"221914_CNY","version":1,"_id": 1672987115,"payload":{"asks": {"amount": [20, 13, 16, 10, 9, 2, 6, 8, 16], "level": [1, 1, 1, 1, 1, 1, 1, 1, 1]},"marketTrades":{"trades": [{"amount": 1, "price": 116, "tid": "T2301061500377140420", "time": 1672988437, "type": "sell"}]},"outerDisc":0
                                                                                                                                                                                                                                                                                                      ,"poor":0, "priceAverage":0,"priceHigh":117,"priceLast":116,"priceLow":112,"priceNew":116,"priceOpen":115,"range":0,"symbolId":"btccny","totalAmount":28888
                                                                                                                                                                                                                                                                                                      ,"totalVolume":403706,"trades":{"amount": [1, 1, 1, 1, 1, 1, 1, 1, 7, 5, 4, 3, 6, 10, 4, 5, 4, 2, 1, 3, 5, 1, 1, 1, 1, 1, 9, 8, 4, 8],"direction":[1, 1, 1, 2, 2, 1, 2, 2, 1, 1, 2, 2, 2, 2, 2, 1, 2, 1, 1, 1, 2, 1, 2, 2, 2, 2, 1, 1, 2, 1]
                                                                                                                                                                                                                                                                                                                                                                                                                                            ,"price": [116, 114, 114, 114, 115, 116, 115, 115, 115, 114, 114, 112, 112, 113, 114, 114, 114, 115, 115, 115]
                                                                                                                                                                                                                                                                                                                                                                                                                                            ,"time": [1672988437, 1672988428, 1672988411, 1672988403, 1672988383, 1672988353, 1672988239, 1672987938]}
                                                                                                                                                                                                                                                                                                      ,"turnVolume":0,"turnoverRate":0,"updownRatio":0,"updownVolume":0,"usdtCount":0,"usdtToRmb":6.8,"volumeRatio":0,"yestdayPriceLast":116
                                                                                                                                                                                                                                                                                                     ,"symbolId":"221319_CNY","version":1,"_id":1672991122,"deep":{}}}]
                                                                          , "deep": {"asks": [[123, 16], [122, 8], [121, 6], [120, 2]], "bids": [[114, 1], [113, 14], [112, 7], [111.2, 10], [111, 13]]}}

            
            await websocket.send(str(json.dumps(ren)))       
        else:
            # 参数错误时的回复
            await  websocket.send("invalid params")
 
async def main():
    # rooter是一个装饰器，它的__call__函数有三个参数，第一个参数是self。
    # 所以这里我们需要使用lambda进行一个转换操作，因为serv的wshander函数只能接收2个参数
    async with websockets.serve(lambda x, y: router(x, y), "127.0.0.1", 7071):
        print("====== WebsocketServer start ====== ")
        await  asyncio.Future()  # run forever

if __name__ == "__main__":
    asyncio.run(main())  
    
    
    
 







 